package com.opencloud.iot.provider.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.opencloud.common.model.PageParams;
import com.opencloud.iot.client.model.entity.*;
import com.opencloud.iot.provider.mapper.IotMoveObserverMapper;
import com.opencloud.iot.provider.mapper.IotNStrengMapper;
import com.opencloud.iot.provider.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 进出记录操作
 * @author yeyong
 */

@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class IotMoveObserverServiceImpl implements IotMoveObserverService {

    @Autowired
    private IotMoveObserverMapper iotMoveObserverMapper;

    @Autowired
    private IotConfigsService iotConfigsService;

    @Autowired
    private IotNStrengMapper iotNStrengMapper;

    @Autowired
    private IotCommonService iotCommonService;

    @Override
    public IPage<IotMoveObserver> findListPage(PageParams pageParams) {
        QueryWrapper<IotMoveObserver> queryWrapper = new QueryWrapper();
        queryWrapper.lambda();
        return iotMoveObserverMapper.selectPage(new Page(pageParams.getPage(),pageParams.getLimit()),queryWrapper);
    }

    public Integer addMoveObserver(IotMoveObserver obj)
    {
        return iotMoveObserverMapper.insert(obj);
    }

    @Override
    public Integer createMoveObserverList(Long startTime, Long endTime) {
        //1、获取配置
        int timeValue = 0;
        int timeSpan = 0;
        double configPercent = 0;
        int configLowValue = 0;
        int configUpValue = 0;
        int Count = 0;
        //1、获取配置规则
        IotConfigs _iotConfigs = iotConfigsService.findConfigByKey("MOVE");
        if(_iotConfigs!=null) {
            timeValue = _iotConfigs.getConfigTimevalue();
            timeSpan = _iotConfigs.getConfigTimespan();
            Count = timeSpan / timeValue;
            configPercent = _iotConfigs.getConfigPercent();
            configLowValue = _iotConfigs.getConfigLowvalue();
            configUpValue = _iotConfigs.getConfigUpvalue();
        }
        //2、根据配置获取原始数据的MAC列表（根据CreateTime获取，每10分钟获取一次）
        List<IotSleepDataTimeZone> nStrengsList = iotNStrengMapper.selectMacAndTimeZone(startTime,endTime);
        //3、数据分组（按MAC）
        //4、取每个分组的数据的最大时间和最小时间 创造时间区间
        for (int n=0;n<nStrengsList.size();n++)
        {
            IotSleepDataTimeZone entry = nStrengsList.get(n);
            String watchmac = entry.getWatchmac();
            Date minTime_date = entry.getMintime();
            Date maxTime_date = entry.getMaxtime();

            // 创造时间区间

            Long minTime_long = iotCommonService.CreateMinTime(timeValue,minTime_date);
            Long maxTime_long = iotCommonService.CreateMaxTime(timeValue,maxTime_date);

            //5、遍历时间区间
            int s = (timeValue*60);
            int x = (int)((maxTime_long-minTime_long)/s);
            for(int i=0;i<x;i++)
            {
                Long stime1 = minTime_long+i*s;
                Long stime2 = minTime_long+(i+1)*s-1;
                //取原始数据
                List<IotMoveObserver> _ObserverList = iotNStrengMapper.selectMoveData(configLowValue,configUpValue,watchmac,stime1,stime2);
                //解析原始数据，形成过程数据
                IotMoveObserver obj = new IotMoveObserver();
                if(_ObserverList.size()>0)
                {
                    System.out.println("有原始数据");
                    IotMoveObserver _m = _ObserverList.get(0);
                    obj.setAvgvalue(_m.getAvgvalue());
                    obj.setStarttime(stime1);
                    obj.setEndtime(stime2);
                    obj.setWatchmac(watchmac);
                    obj.setLowcounts(_m.getLowcounts());
                    obj.setMiddlecounts(_m.getMiddlecounts());
                    obj.setHighcounts(_m.getHighcounts());
                    this.iotMoveObserverMapper.insert(obj);
                }
            }
        }
        return nStrengsList.size();
    }
}
